package ai;

import java.util.HashMap;

import loadsave.LoadSaveable;
import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveMapComposite;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;
import map.GameWorld;
import scheduler.Notifiable;
import entity.Avatar;
import entity.Entity;

public abstract class AI implements LoadSaveable {

	private HashMap<Entity,Notifiable> enemies = new HashMap<Entity, Notifiable>();
	private Avatar avatar;
	
	public abstract boolean addEntity(Entity entity);
	public abstract void removeEntity(Entity entity);
	public abstract void removeAll();
	
	protected HashMap<Entity, Notifiable> getEnemies() {
		return enemies;
	}
	
	protected void setEnemies(HashMap<Entity, Notifiable> m) {
		enemies = m;
	}
	
	protected Avatar getAvatar() {
		return avatar;
	}

	protected void setAvatar(Avatar avatar) {
		this.avatar = avatar;
	}
	
	@Override
	public Memento getMemento() {
		return new Memento() {

			@Override
			public LoadSaveObject getState() {
				LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "AI");
				LoadSaveMapComposite lsmc = new LoadSaveMapComposite("enemies", "HashMap", "Entity", "Notifiable");
				for (Entity e : enemies.keySet()) {
					lsmc.add(new LoadSavePrimitive("", "", e.getName()), new LoadSavePrimitive("", "", ""));
				}
				toReturn.add(lsmc.getName(), lsmc);
				return toReturn;
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				setAvatar(GameWorld.getInstance().getAvatar());
				Iterator<LoadSaveObject> i = lso.get("enemies").getIterator();
				for (i.init(); i.hasNext(); i.next()) {
					addEntity(GameWorld.getInstance().getEntity(i.getCurrKey().getValue()));
				}
			}
			
		};
	}
	
}
